<template>
  <div>
    <div class="menu">
      <ul class="menulist">
        <li v-for="(item, key) in catemenu" :key="item.id" class="li1" @mouseenter="index = key">
          <RouterLink to="/category" class="a">{{ item.name }}</RouterLink>
          <RouterLink to="/" v-for="it in item.children" :key="it.id">{{
              it.name
          }}</RouterLink>
          <div class="showcate">
            <h4>{{ categoods.keys && categoods.keys == 'cate' ? '分类推荐' : '品牌推荐' }} <small>根据您的购买或浏览记录推荐</small></h4>
            <ul v-if="categoods.keys && categoods.keys == 'cate'">
              <li v-for="item in categoods.goods" :key="item.id">
                <RouterLink to="/" class="li">
                  <img :src="item.picture" alt="" />
                  <div class="info">
                    <p class="name ellipsis-2">{{ item.name }}</p>
                    <p class="desc ellipsis">{{ item.desc }}</p>
                    <p class="price"><i>¥</i>{{ item.price }}</p>
                  </div>
                </RouterLink>
              </li>
            </ul>
            <ul v-if="categoods.keys && categoods.keys == 'brand'">
              <li v-for="item in categoods.goods" :key="item.id">
                <RouterLink to="/" class="li">
                  <img :src="item.picture" alt="" />
                  <div class="info">
                    <p class="name ellipsis-2">{{ item.name }}</p>
                    <p class="desc ellipsis">{{ item.desc }}</p>
                    <p class="price"><i>¥</i>{{ item.id }}</p>
                  </div>
                </RouterLink>
              </li>
            </ul>
          </div>
        </li>
      </ul>
      <div class="swiper">
        <img :src="item.imgUrl" alt="" v-for="(item, key) in bannerList" :key="item.id" :class="{ flag: key == imgA }">
        <p>
          <span :class="imgA == key ? 'showSpan' : 'span'" v-for="(item, key) in bannerList" :key="key"></span>
        </p>
        <div class="yuan1" @click="taggerLeft">&gt;</div>
        <div class="yuan2" @click="taggerRight">&lt;</div>
      </div>
    </div>
  </div>
</template>
<script>
import { getbrand } from '@/utils/api/homeapi'
import { getBanner } from '@/utils/api/homeapi'
export default {
  data() {
    return {
      index: 0,
      BrandList: [],
      bannerList: [],
      imgA: 0,
      timer: null
    }
  },
  methods: {
    async getBrandList() {
      let res = await getbrand(6)
      this.BrandList = res.result
      // console.log(this.BrandList);
    },
    /* 获取轮播图数据  */
    async getBannerList() {
      let res = await getBanner();
      this.bannerList = res.result;
    },
    /* 轮播图手动切换 */
    taggerLeft() {
      if (this.imgA != 0) {
        this.imgA--;
      } else {
        this.imgA = 4;
      }
    },
    /* 轮播图手动切换 */
    taggerRight() {
      if (this.imgA != 4) {
        this.imgA++;
      } else {
        this.imgA = 0;
      }
    },
  },
  mounted() {
    this.getBrandList();
    this.getBannerList();
    this.timer = setInterval(() => {
      this.imgA++;
      if (this.imgA >= this.bannerList.length) {
        this.imgA = 0;
      }
    }, 1000)
  },
  computed: {
    catemenu() {
      let obj = {
        id: '1221',
        name: '品牌',
        children: [
          {
            id: '122112',
            name: '品牌推荐'
          }
        ]
      }
      let list = this.$store.state.category.cate.map(item => {
        return {
          id: item.id,
          name: item.name,
          children: item.children && item.children.slice(0, 2)
        }
      })
      list.push(obj)
      return list
    },
    categoods() {
      let obj = {
        keys: '',
        goods: []
      }
      if (this.index <= 8) {
        obj.keys = 'cate'
        obj.goods = this.$store.state.category.cate[this.index].goods
      } else {
        obj.keys = 'brand'
        obj.goods = this.BrandList
      }
      return obj
    }
  },
  beforeDestroy() {
    //清除定时器
    clearInterval(this.timer);
    this.timer = null;
  }
}
</script>
<style lang="scss" scoped>
.menu {
  width: 1240px;
  height: 500px;
  background: #ccc;
  position: relative;
  white-space: pre-line;
  word-break: break-all;
  word-wrap: break-word;

  .menulist {
    width: 250px;
    background: #231f1b;
    opacity: 0.8;
    height: 500px;
    position: absolute;
    left: 0;
    top: 0;
    // background: salmon;
    z-index: 999;

    // position: relative;
    .li1 {
      height: 50px;
      line-height: 50px;
      padding-left: 30px;
      overflow: hidden;
      white-space: pre-line;
      word-break: break-all;
      word-wrap: break-word;

      >.a {
        color: #fff;
        font-size: 17px;
        margin-right: 5px;
      }

      >a {
        color: #fff;
      }

      &:hover {
        background: #27ba9b;

        .showcate {
          display: block;
        }
      }
    }
  }

  .showcate {
    background: rgba($color: #ffffff, $alpha: 0.2);
    height: 500px;
    position: absolute;
    left: 250px;
    top: 0;
    width: 990px;
    display: none;

    >h4 {
      font-size: 40px;
      margin-left: 25px;

      >small {
        font-size: 25px;
        // color: #ddd;
      }
    }

    >ul {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      align-items: center;
      height: 444px;

      >li {
        line-height: 20px;
        display: flex;
        margin-right: 13px;
        width: 310px;
        height: 120px;
        border: 1px solid #ddd;
        border-radius: 5px;
        background: #fff;
        white-space: pre-line;
        word-break: break-all;
        word-wrap: break-word;
        overflow: auto;

        img {
          margin: 10px 10px;
          width: 100px;
          height: 100px;
        }

        >.li {
          display: flex;
        }

        .name {
          font-size: 15px;
          font-weight: 900;
          color: #000;
        }

        .desc {
          color: rgb(159, 155, 155);
          color: #000;
        }

        .price {
          i {
            font-size: 20px;
          }

          color: #000;
          font-size: 30px;
          color: #bf4e49;
        }

        &:hover {
          background: #27ba9b;
        }
      }
    }
  }

  .swiper {
    width: 100%;
    height: 500px;
    // background-color: red;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 990;
    opacity: 0.8;

    img {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
    }

    p {
      width: 200px;
      height: 40px;
      // background-color: red;
      z-index: 9999;
      position: absolute;
      left: 700px;
      bottom: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      span {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background-color: white;
        cursor: pointer;
      }

      span:hover {
        background-color: #231f1b;
      }

      .showSpan {
        background-color: #231f1b;
      }
    }

    .flag {
      opacity: 1;
      z-index: 1000;
    }

    .yuan1 {
      width: 60px;
      height: 60px;
      background-color: black;
      opacity: 0.7;
      border-radius: 50%;
      z-index: 9999;
      position: absolute;
      top: 50%;
      right: 30px;
      transform: translate(0, -50%);
      font-size: 40px;
      color: white;
      text-align: center;
      cursor: pointer;
    }

    .yuan2 {
      width: 60px;
      height: 60px;
      background-color: black;
      opacity: 0.7;
      border-radius: 50%;
      z-index: 9999;
      position: absolute;
      top: 50%;
      left: 280px;
      transform: translate(0, -50%);
      font-size: 40px;
      color: white;
      text-align: center;
      cursor: pointer;
    }
  }
}
</style>
